package com.naihe.anderson.sms.manager;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.text.TextUtils;
import android.widget.RemoteViews;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.activity.MainSmsActivity;
import com.naihe.anderson.sms.activity.TalkMessageActivity;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.ContactUser;
import com.naihe.anderson.sms.entity.NhsmsSession;

public class NotificationMgr {

	public static int LOGO_ID = 20120629;
	
	NotificationManager notificationManager;
	NhSmsApp application;
	
	private Vibrator vibrator;
    private Ringtone ringtone;
	long[] pattern = {0,300};
	
	public NotificationMgr(NhSmsApp app){
		this.application = app;
		notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
	}
	private long oldSessionID = -1;
	private int currentNum = 0;
	
	public void showNotification(int num, NhsmsSession nhsmsSession, long currentNhsmsThreadId){
		currentNum += num;
		if(nhsmsSession == null 
				|| (nhsmsSession != null && !TextUtils.isEmpty(nhsmsSession.getCipher())) 
				|| !TextUtils.isEmpty(application.getNhSmsMgr().getBaseSafetyCipher())){
			showNumalNotification(currentNum);
		} else {
			if(oldSessionID == nhsmsSession.getTid() && currentNhsmsThreadId != nhsmsSession.getTid()){
				showPersonalNotification(currentNum, nhsmsSession);
			} else if(oldSessionID == nhsmsSession.getTid() && currentNhsmsThreadId == nhsmsSession.getTid()){
				showMnsNotificationImmediatelyVibrator();
				currentNum -= num;
			} else if(oldSessionID == -1 && currentNum == 1){
				showPersonalNotification(currentNum, nhsmsSession);
			} else {
				showNumalNotification(currentNum);
			}
			
			oldSessionID = nhsmsSession.getTid();
		}
	}
	
	private void showNumalNotification(int num){
		if(!application.getNhSmsMgr().getBaseShowNotif()){
			return;
		}
		
		Intent intent = new Intent();
		intent.setClass(application, MainSmsActivity.class);
		
		Notification notification = new Notification(R.drawable.mns_logo, 
													application.getResources().getString(R.string.mns_notification_text), 
													System.currentTimeMillis());
		PendingIntent contentIntent = PendingIntent.getActivity(application, 0, intent, 0);
		int defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
//		if(application.getNhSmsMgr().getBaseGetMsgRing()){
//			defaults = Notification.DEFAULT_SOUND | defaults;
//		}
		if(application.getNhSmsMgr().getBaseGetMsgVibrator()){
			defaults = Notification.DEFAULT_VIBRATE | defaults;
		}
		notification.defaults = defaults;
		notification.number = num;
		
		RemoteViews contentView=new RemoteViews(application.getPackageName(), R.layout.notification_layout);  
		contentView.setOnClickPendingIntent(R.id.image_layout, contentIntent);
		contentView.setTextViewText(R.id.notification_info_text, application.getResources().getString(R.string.no_readnum, num));
		contentView.setTextViewText(R.id.notification_title, application.getResources().getString(R.string.mns_notification_title));
	    notification.contentView=contentView;
		
//		AudioManager mAudioManager = (AudioManager) application.getSystemService(Context.AUDIO_SERVICE);
//        boolean shouldVibrate = mAudioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION);
//        if (!shouldVibrate && getBaseGetMsgVibrator()) {
//            if (vibrator == null) {
//                vibrator = (Vibrator) application.getSystemService(Context.VIBRATOR_SERVICE);
//            }
//            vibrator.vibrate(pattern, -1);
//        }
        
		notificationManager.notify(LOGO_ID, notification);
	}
	
	private void showPersonalNotification(int num, NhsmsSession nhsmsSession){
		if(!application.getNhSmsMgr().getBaseShowNotif()){
			return;
		}
		
		Intent intent = new Intent();
		intent.setClass(application, TalkMessageActivity.class);
		intent.putExtra("NhsmsSession", nhsmsSession);
		PendingIntent contentIntent = PendingIntent.getActivity(application, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		ContactUser contactUser = application.getContactMgr().getContactUserById(application.getContactMgr().getContactIdByPhoneNum(nhsmsSession.getAddress()));
		
		Notification notification = new Notification(R.drawable.mns_logo, 
				application.getNhSmsMgr().getBaseShowNewSmsBody() ? nhsmsSession.getBody() : application.getResources().getString(R.string.mns_notification_text), 
				System.currentTimeMillis());
		RemoteViews contentView=new RemoteViews(application.getPackageName(), R.layout.notification_layout);  
		contentView.setOnClickPendingIntent(R.id.image_layout, contentIntent);
		
		if(application.getNhSmsMgr().getBaseShowNewSmsBody() && num == 1){
			contentView.setTextViewText(R.id.notification_info_text, nhsmsSession.getBody());
		} else {
			contentView.setTextViewText(R.id.notification_info_text, application.getResources().getString(R.string.no_readnum, num));
		}
		
		if(contactUser != null){
			if(application.getNhSmsMgr().getBaseShowNewSmsName()){
				contentView.setTextViewText(R.id.notification_title, contactUser.getName());  
			} else {
				contentView.setTextViewText(R.id.notification_title, application.getResources().getString(R.string.mns_notification_title));
			}
			
			if(application.getNhSmsMgr().getBaseShowNewSmsHeadImage()){
				Bitmap bitmap = application.getContactMgr().getContactHeadImgByPhotoId(contactUser.getPhotoId());
				if(bitmap != null){
					contentView.setImageViewBitmap(R.id.notification_image, bitmap);  
				}
			}
		} else {
			contentView.setTextViewText(R.id.notification_title, nhsmsSession.getAddress());
		}
	    notification.contentView=contentView;
	    
		int defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
//		if(application.getNhSmsMgr().getBaseGetMsgRing()){
//			defaults = Notification.DEFAULT_SOUND | defaults;
//		}
		if(application.getNhSmsMgr().getBaseGetMsgVibrator()){
			defaults = Notification.DEFAULT_VIBRATE | defaults;
		}
		notification.defaults = defaults;
		notification.number = num;
		notificationManager.notify(LOGO_ID, notification);
	}
	
	private void showMnsNotificationImmediatelyVibrator(){
		if(vibrator == null){
			vibrator = (Vibrator) application.getSystemService(Context.VIBRATOR_SERVICE);
		}
		if(application.getNhSmsMgr().getBaseGetMsgVibrator()){
			vibrator.vibrate(pattern, -1);
		}
		if(ringtone == null){
			Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
		    ringtone = RingtoneManager.getRingtone(application, notification);
		}
		ringtone.play();
	}
	
	public void cancelNotif(){
		currentNum = 0;
		notificationManager.cancel(LOGO_ID);
	}
	
	public void setOldSessionID(long oldSessionID){
		this.oldSessionID = oldSessionID;
	}
}
